package com.wnhz.work.yz.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.OtherAddGroupBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.CircularImage;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.x;

import java.util.HashMap;
import java.util.Map;

public class OtherDetailActivity extends BaseActivity implements View.OnClickListener {
    private CircularImage head_img;
    private TextView name, other_info;
    private TextView team_names, add_time, jujue, tongyi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_other_detail);
        initTitle();
        initData();
        initView();

    }

    private void initView() {
        //  private CircularImage head_img;
        head_img = (CircularImage) findViewById(R.id.head_img);
        name = (TextView) findViewById(R.id.name);
        other_info = (TextView) findViewById(R.id.other_info);
        team_names = (TextView) findViewById(R.id.team_name);
        add_time = (TextView) findViewById(R.id.add_time);
        jujue = (TextView) findViewById(R.id.jujue);
        jujue.setOnClickListener(this);
        tongyi = (TextView) findViewById(R.id.tongyi);
        tongyi.setOnClickListener(this);
        setData();
    }

    private void setData() {
        x.image().bind(head_img, info.getUser_img());
        name.setText(info.getUser_name());
        other_info.setText(info.getUser_sex() + "\t\t" + info.getUser_age() + "岁\t\t" + info.getUser_address());
        team_names.setText(team_name + "");
        add_time.setText(info.getAdd_time());
    }

    private void initTitle() {
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("申请通知");
        //title.setTextSize(20);
        title.setTextColor(getResources().getColor(R.color.main_color));
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }

    private OtherAddGroupBean.InfoEntity info;
    private String team_name;
    private String team_id;

    private void initData() {
        info = (OtherAddGroupBean.InfoEntity) getIntent().getSerializableExtra("bean");
        team_name = getIntent().getStringExtra("team_name");
        team_id = getIntent().getStringExtra("team_id");

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.jujue:
                upLoadJuJue(true);
                break;
            case R.id.tongyi:
                upLoadJuJue(false);
                break;
        }
    }

    private void upLoadJuJue(boolean flag) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("team_id", team_id);
        map.put("user_id", info.getUser_id());
        showDialog();

        String url = "";
        if (flag) {
            url = Url.TEAM_REFUSEDADDTEAM;
        } else {
            url = Url.TEAM_AGREEADDTEAM;
        }

        XUtil.Post(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }


}
